Guest User

yandex rsa

a guest
Feb 26th, 2010
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <?php
  2.    
  3.     class yandex_photo{
  4.         private $rsa_key = null;
  5.         private $request_id = null;
  6.         private $token = null;
  7.         private $login = null;
  8.         private $password = null;
  9.         private $last_message = null;
  10.         public function __construct($login=null,$password=null){
  11.             if($login==null||$password==null){
  12.                 throw new Exception("Не заданы данные аутентификации",E_ERROR);
  13.             }
  14.             $this->login = $login;
  15.             $this->password = $password;
  16.         }
  17.        
  18.  
  19.     public function encrypt_yarsa2($key, $data){
  20.  
  21.       $buffer = array();
  22.       list($nstr, $estr) = explode('#', $key);
  23.       $n = gmp_init('0x' .$nstr);
  24.       $e = gmp_init($estr);
  25.       $stepSize = strlen($nstr) / 2 - 1;
  26.      
  27.  
  28.       $prev_crypted = array();
  29.       $prev_crypted = array_fill(0, $stepSize, 0);
  30.       $hex_out = '';
  31.       for($i = 0; $i < strlen($data); ++$i)
  32.       {
  33.         $buffer[$i] = ord($data{$i});
  34.       }
  35.       for ($i = 0; $i < intval((count($buffer) - 1) / $stepSize) + 1; ++$i)
  36.       {
  37.         $tmp = array_slice($buffer, $i * $stepSize, ($i + 1) * $stepSize);
  38.         for ($j = 0; $j < count($tmp); ++$j)
  39.         {
  40.           $tmp[$j] = ($tmp[$j] ^ $prev_crypted[$j]);
  41.         }
  42.         $tmp = array_reverse($tmp);
  43.         $plain = gmp_init(0);
  44.         for($x = 0; $x < count($tmp); ++$x)
  45.         {
  46.           $pow = gmp_powm(gmp_init(256), gmp_init($x), $n);
  47.           $pow_mult = gmp_mul($pow, gmp_init($tmp[$x]));
  48.           $plain = gmp_add($plain, $pow_mult);
  49.         }
  50.         $plain_pow = gmp_powm($plain, $e, $n);
  51.         $plain_pow_str = gmp_strval($plain_pow, 16);
  52.         $hex_result = array_fill(0, array(strlen($nstr) - strlen($plain_pow_str)), '0');
  53.         $hex_result = implode($hex_result) + $plain_pow_str;
  54.         $min_x = min(strlen($hex_result), count($prev_crypted) * 2);
  55.         for ($x = 0; $x < $min_x; $x = $x + 2)
  56.         {
  57.           $prev_crypted[$x / 2] = hexdec('0x' + substr($hex_result, $x, 2));
  58.         }
  59.         if(count($tmp) < 16)
  60.         {
  61.           $hex_out += '00';
  62.         }
  63.         $hex_out += dechex(count($tmp)) + '00';
  64.         $ks = strlen($nstr) / 2;
  65.         if($ks < 16)
  66.         {
  67.           $hex_out += '0';
  68.         }
  69.         $hex_out += dechex($ks) + '00';
  70.         $hex_out += $hex_result;
  71.  
  72.       }
  73.       return base64_encode(pack("H*" , $hex_out));
  74.     }
  75.        
  76.         public function encrypt_yarsa($key, $toenc){
  77.             $data_arr = array();
  78.             $nstr = explode("#",$key);
  79.             $estr = $nstr[1];
  80.             $nstr = $nstr[0];
  81.             $n = hexdec($nstr);
  82.             $e = hexdec($estr);
  83.            
  84.             $step_size = strlen($nstr)/2-1;
  85.             $i=$step_size;
  86.             while($i--){
  87.                 $prev_crypted[]="";
  88.             }
  89.             $hex_out = "";
  90.             $plain = "";
  91.            
  92.             for($i=0;$i<strlen($toenc);$i++){
  93.                 $data_arr[] = ord(substr($toenc,$i,1));
  94.             }
  95.            
  96.             for($i=0;$i<(int)((count($data_arr)-1)/$step_size)+1;$i++){
  97.                
  98.                 $tmp = array_slice($data_arr,$i*$step_size,(($i+1)*$step_size) - ($i*$step_size));
  99.                 for($j=0;$j<count($tmp);$j++){
  100.                     $tmp[$j] = $tmp[$j]^$prev_crypted[$j];
  101.                 }
  102.                 $tmp = array_reverse($tmp);
  103.                
  104.  
  105.                 $plain = 0;            
  106.                 for($x=0;$x<count($tmp);$x++){
  107.                     $pow = $this->m_powmod(256,$x,$n);
  108.                     $pow_mult = $pow*$tmp[$x];
  109.                     $plain = $plain+$pow_mult;
  110.                 }
  111.                 //Пока все совпадает
  112.                
  113.                 //plain_pow = powMod(plain, str2bigInt(E,10,0), str2bigInt(N,10,0));
  114.                 $plain_pow = $this->m_powmod($plain,$e,$n);
  115.                 echo "<br/>plain: ";
  116.                 echo $plain;
  117.                 echo "<br/>e: ";
  118.                 echo $e;
  119.                 echo "<br/>n: ";
  120.                 echo $n;
  121.                 echo "<br/>plain_pow: ";
  122.                 echo $plain_pow;
  123.                 echo "<br/>";
  124.  
  125.                 /*
  126.                
  127.                
  128.                 $plain_pow_str = (string)dechex($plain_pow);
  129.                 $hex_result = array();
  130.                 $i=$step_size;
  131.                 while($i--){
  132.                     $hex_result[]="";
  133.                 }
  134.                 $hex_result = implode("0",$hex_result).$plain_pow_str;
  135.                 $min_x = min(strlen($hex_result), count($prev_crypted)*2);
  136.                 for($x=0;$x<$min_x;$x=$x+2){
  137.                     $prev_crypted[$x/2] = (int)("0x"+substr($hex_result,$x, 2));
  138.                 }
  139.                 if(count($tmp)<16){
  140.                     $hex_out.="00";
  141.                 }
  142.                 $hex_out.= strtoupper(dechex(count($tmp)))."00";
  143.                 $ks = strlen((string)$nstr)/2;
  144.                 if($ks<16){
  145.                     $hex_out.="0";
  146.                 }
  147.                 $hex_out.= strtoupper(dechex($ks))."00";
  148.                 $hex_out.= $hex_result;
  149.                 */
  150.             }
  151.             $pattern = "|[\n\r\t]+|usi";
  152.             return preg_replace($pattern,"",base64_encode(hexdec(strtolower($hex_out))));
  153.         }
  154.        
  155.         private function m_mod($a,$b){
  156.             return floor($a)-floor($a/$b)*$b;
  157.         }
  158.        
  159.         private function m_powmod($d, $p, $m){
  160.             for ($b = 1; $p;){
  161.                 if ($p & 1){
  162.                     $p--;
  163.                     $b = $this->m_mod($b * $d, $m);
  164.                 }else{
  165.                     $p >>= 1;
  166.                     $d = $this->m_mod($d * $d, $m);
  167.                 }
  168.             }
  169.             return $b;
  170.         }
  171.        
  172.         public function authentication(){
  173.             $curl = curl_init();
  174.             curl_setopt($curl, CURLOPT_URL, "http://auth.mobile.yandex.ru/yamrsa/key/");
  175.             curl_setopt($curl, CURLOPT_HEADER, false);
  176.             curl_setopt($curl, CURLOPT_HTTPGET, true);
  177.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  178.             echo $this->last_message = curl_exec($curl);
  179.             curl_close($curl);
  180.         }
  181.     }
  182. /*
  183. <?xml version="1.0"?>
  184. <response>
  185.   <key>962F27B863D09CC600374CD7705F4DA134C1B69400DCB077E528B20F388AA8C427DD7D26BFA20AC55F7EEAC45F826593D411CDF7746E6E097E2D1CB1EB3B38E9#10001</key>
  186.   <request_id>cbb9ad4f3485425bb7e95df46fb1ea51</request_id>
  187. </response>
  188. */
  189.    
  190.     header('content-type:text/html; charset=utf-8');
  191.     $photo = new yandex_photo("silentimp","fkytnyjfy_2002");
  192.     //$photo->authentication();
  193.     echo $photo->encrypt_yarsa("962F27B863D09CC600374CD7705F4DA134C1B69400DCB077E528B20F388AA8C427DD7D26BFA20AC55F7EEAC45F826593D411CDF7746E6E097E2D1CB1EB3B38E9#10001",'<credentials login="mojo" password="qwerty"/>');
  194.    
  195. ?>
Advertisement
Add Comment
Please, Sign In to add comment